package com.ruanyun.modular.ry.api.web;

import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.ruanyun.api.core.util.UserLoginToken;
import com.ruanyun.modular.ry.api.vo.req.AddressInsertReq;
import com.ruanyun.modular.ry.api.vo.req.AddressUpdateReq;
import com.ruanyun.modular.ry.api.vo.req.PageReq;
import com.ruanyun.modular.ry.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Validated
@CrossOrigin
@RestController
@RequestMapping("/api/address")
public class ApiAddressController extends RyController {

    @Autowired
    private AddressService addressService;

    /**
     * 分页列表
     *
     * @param pageReq
     * @return
     */
    @PostMapping("/findPage")
    public ResponseData findPage(@Valid @RequestBody PageReq pageReq) {
        return this.addressService.findPage(pageReq.getCurrent(), pageReq.getSize(), getCustomerId());
    }

    /**
     * 地址信息
     *
     * @param id
     * @return
     */
    @PostMapping("/detail")
    public ResponseData detail(@RequestBody @NotNull(message = "地址id不能为空") Long id) {
        return ResponseData.success(this.addressService.getById(id));
    }

    /**
     * 添加地址
     *
     * @param addressInsertReq
     * @return
     */
    @PostMapping("/insert")
    public ResponseData insertAddress(@Valid @RequestBody AddressInsertReq addressInsertReq) {
        return this.addressService.insertAddress(addressInsertReq, getCustomerId());
    }

    /**
     * 修改地址
     *
     * @param addressUpdateReq
     * @return
     */
    @PostMapping("/update")
    public ResponseData updateAddress(@Valid @RequestBody AddressUpdateReq addressUpdateReq) {
        return this.addressService.updateAddress(addressUpdateReq, getCustomerId());
    }

    /**
     * 删除地址
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public ResponseData deleteAddress(@NotNull(message = "参数不能为空") @RequestParam(value = "id") Long id) {
        return this.addressService.deleteAddressById(id);
    }

    /**
     * 获取用户默认地址
     *
     * @return
     */
    @PostMapping("/default")
    @UserLoginToken
    public ResponseData getDefaultAddress() {
        return this.addressService.getDefaultAddressByCustomerId(getCustomerId());
    }
}
